// const sass = require("node-sass");
// const sass = require("sass");

const { option } = require("grunt");

const data = {
  menus: [
    {
      name: "Home",
      icon: "aperture",
      link: "index.html",
    },
    {
      name: "Features",
      link: "features.html",
    },
    {
      name: "About",
      link: "about.html",
    },
    {
      name: "Contact",
      link: "#",
      children: [
        {
          name: "Twitter",
          link: "https://twitter.com/w_zce",
        },
        {
          name: "About",
          link: "https://weibo.com/zceme",
        },
        {
          name: "divider",
        },
        {
          name: "About",
          link: "https://github.com/zce",
        },
      ],
    },
  ],
  pkg: require("./package.json"),
  date: new Date(),
};

module.exports = (grunt) => {
  // 自动加载所有的 grunt 插件中的任务
  require("load-grunt-tasks")(grunt);
  const serveStatic = require("serve-static");

  grunt.initConfig({
    meta: {
      stylePath: "assets/styles/",
    },

    clean: {
      temp: "temp/**",
      dist: "dist/**",
    },

    sass: {
      dist: {
        options: {
          style: "expanded",
        },
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["**/*.scss"],
            dest: "dist",
            ext: ".scss",
          },
        ],
      },
    },

    babel: {
      options: {
        sourceMap: true,
        presets: ["@babel/preset-env"],
      },
      dist: {
        files: [
          {
            expand: true,
            cwd: "src",
            src: ["**/*.js"],
            dest: "dist",
            ext: ".js",
          },
        ],
      },
    },

    swigtemplates: {
      options: {
        defaultContext: {
          ...data,
          pageTitle: "My Title",
        },
        templatesDir: "src/",
      },
      production: {
        dest: "dist",
        src: ["src/**/*.html"],
      },
      staging: {
        context: {
          pageTitle: "My Title (staging)",
        },
        dest: "build/staging/",
        src: ["src/swig/**/*.swig"],
      },
    },

    copy: {
      main: {
        files: [
          {
            expand: true,
            src: "**/fonts/*",
            dest: "dist/",
            cwd: "src/",
          },
          {
            expand: true,
            src: "**/images/*",
            dest: "dist/",
            cwd: "src/",
          },
          {
            expand: true,
            src: "**",
            dest: "dist/",
            cwd: "public/",
          },
        ],
      },
    },

    connect: {
      server: {
        options: {
          port: 8089, //服务器的端口号，可用localhost:9000访问
          // Change this to '0.0.0.0' to access the server from outside.
          keepalive: true,
          hostname: "localhost", //服务器域名
          livereload: 35729, //声明给watch监听的端口
          open: true, //true：一直打开链接，不然浏览器只打开一次
          base: ["dist", "src", "public"],

          //   middleware: function (connect) {

          //   },
        },
      },
    },

    watch: {
      livereload: {
        options: {
          livereload: "<%=connect.options.livereload%>", //监听前面声明的端口  35729
        },

        files: [
          //下面文件的改变就会实时刷新网页
          //   "app/*.html",
          //   "app/style/{,*/}*.css",
          //   "app/scripts/{,*/}*.js",
          //   "app/images/{,*/}*.{png,jpg}",
          "src/*",
          "public/*",
        ],
      },
    },
  });

  //   grunt.loadNpmTasks("grunt-contrib-clean");
  //   grunt.loadNpmTasks("grunt-sass");
  //   grunt.loadNpmTasks("grunt-contrib-sass");
  //   grunt.loadNpmTasks("grunt-babel");

  grunt.registerTask("default", [
    "clean",
    "sass",
    "babel",
    "swigtemplates",
    "copy",
    "connect",
  ]);
};
